package com.hdax.service.Impl;

import com.hdax.entity.Attendance;
import com.hdax.entity.Role;
import com.hdax.entity.State;
import com.hdax.entity.Wages;
import com.hdax.mappers.AttendanceMapper;
import com.hdax.mappers.WagesMapper;
import com.hdax.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 工资服务impl
 *
 * @author Aizen
 * @date 2022/06/04
 */
@Service
public class WagesServiceImpl implements WagesService {

    @Autowired
    private WagesMapper wagesMapper;

    @Autowired
    private AttendanceService attendanceService;

    @Autowired
    private StateService stateService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    @Override
    public List<Wages> findAll(int offset, int limit, Wages wages) {
        System.out.println(wages.toString());
        return wagesMapper.findAll(offset,limit,wages);
    }

    @Override
    public int CountByname(Wages wages) {
        return wagesMapper.CountByname(wages);
    }

    @Override
    public int Count() {
        return wagesMapper.Count();
    }

    @Override
    public int insertById(Wages wages) {
        //获取当前时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());

       String wagesDate=formatter.format(date);
        wages.setMoneydate(wagesDate);//当前时间

        //全勤工资
        Double fullwages=0.0;
        //迟到工资
        Double late=0.0;
        //请假工资
        Double leave=0.0;
        //旷工工资
        Double Absenteeism=0.0;
        //应发工资
        Double countall=0.0;
        //实发工资
        Double shifa=0.0;
        //个税
        Double geshui=0.0;
        //五险一金
        Double wuxian=0.0;


        State state=new State();

        Attendance attendance=new Attendance();
        attendance.setEmployeeid(wages.getEmployeeid());//员工编号
        //截取字符串
        String moneydates=wagesDate.substring(0,7);
        attendance.setDakadateone(moneydates);//当前月份


        //对应员工的基础工资
        int rid= employeeInfoService.findEmployeeInfoById(wages.getEmployeeid()).getRoleId();
        Role role=roleService.findById(rid);
        Double money= Double.valueOf(role.getBasepay());


        //迟到次数
        int letecount=attendanceService.CountState(attendance,1);
        //请假次数
        int leavecount=attendanceService.CountState(attendance,2);
        //旷工次数
        int absenteeismcount=attendanceService.CountState(attendance,3);
        if (letecount>0){
            state=stateService.findByid(1);
            double cishu= Double.parseDouble(state.getPayment());//当前迟到的金额
            late=letecount*cishu;
        }else if (leavecount>0){
            state=stateService.findByid(2);
            double cishu1= Double.parseDouble(state.getPayment());//当前请假的金额
            leave=leavecount*cishu1;
        }else if (absenteeismcount>0){
            state=stateService.findByid(3);
            double cishu2= Double.parseDouble(state.getPayment());//当前旷工的金额
            Absenteeism=absenteeismcount*cishu2;
            if(money*0.2<Absenteeism){
                Absenteeism=money*0.2;
            }
        }else{
            state=stateService.findByid(6);
            fullwages= Double.valueOf(state.getPayment());
        }

        //五险一金
        state=stateService.findByid(5);
        wuxian= Double.valueOf(state.getPayment());

        String eid=employeeInfoService.findEmployeeInfoById(wages.getEmployeeid()).getEmpZhuanZhengDate();
        if (eid!=null || eid!=""){
            countall=money+fullwages-late-leave-Absenteeism-wuxian;
            if (0<countall  && countall<3000){
                geshui=0.0;
            }else if (3000<countall  && countall<12000){
                geshui=countall*0.1;
            }else if (12000<countall  && countall<25000){
                geshui=countall*0.2;
            }else if (25000<countall  && countall<35000){
                countall=countall*0.25;
            }else if (35000<countall  && countall<55000){
                countall=countall*0.30;
            }else if (55000<countall  && countall<8000){
                countall=countall*0.35;
            }else if (80000<countall){
                countall=countall*0.40;
            }
            shifa=countall-geshui;
        }else{
            countall=(money+fullwages-late-leave-Absenteeism-wuxian)*0.8;
            if (0<countall  && countall<3000){
                geshui=0.0;
            }else if (3000<countall  && countall<12000){
                geshui=countall*0.1;
            }else if (12000<countall  && countall<25000){
                geshui=countall*0.2;
            }else if (25000<countall  && countall<35000){
                countall=countall*0.25;
            }else if (35000<countall  && countall<55000){
                countall=countall*0.30;
            }else if (55000<countall  && countall<8000){
                countall=countall*0.35;
            }else if (80000<countall){
                countall=countall*0.40;
            }
            shifa=countall-geshui;
        }
        wages.setFullwages(fullwages);//全勤工资
        wages.setBasemoney(money);//基础工资
        wages.setLate(late);//迟到工资
        wages.setLeave(leave);//请假工资
        wages.setAbsenteeism(Absenteeism);//旷工工资
        wages.setTax(geshui);//个税
        wages.setRisk(wuxian);//五险一金
        wages.setShifamoney(shifa);//实发工资
        wages.setEstablishtime(wagesDate);
        return wagesMapper.insertById(wages);
    }

    @Override
    public List<Wages> CountMoney() {
        return wagesMapper.CountMoney();
    }

    @Override
    public Wages selectById(int id) {
        return wagesMapper.selectById(id);
    }

    @Override
    public List<Wages> insertDate(String moneydate) {
        return wagesMapper.insertDate(moneydate);
    }

    @Override
    public int deleByid(int id) {
        return wagesMapper.deleByid(id);
    }

    @Override
    public List<Wages> findByeid(int employeeid) {
        return wagesMapper.findByeid(employeeid);
    }

    @Override
    public Wages sumwage(String moneydate) {
        return wagesMapper.sumwage(moneydate);
    }

}
